/**
 * 保存数据至本地
 * @param key
 * @param value
 */
export const localSave = (key: string, value: any) => {
  let data = value;
  if (typeof data === 'object') {
    data = JSON.stringify(value);
  }
  localStorage.setItem(key, data);
};

/**
 * 读取本地数据
 * @param key
 * @returns
 */
export const localRead = (key: string) => {
  const data = localStorage.getItem(key) || '';
  try {
    return JSON.parse(data);
  } catch (err) {
    return data;
  }
};

/**
 * 移除本地数据
 * @param key
 */
export const localRemove = (key: string) => {
  localStorage.removeItem(key);
};

/**
 * 判断链接是否非法
 */
const reg =
  /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export const isUrl = (path: string) => {
  return reg.test(path);
};

/**
 * -转驼峰
 * @param val str
 * @returns string
 */
export const strikethroughToHump = (val: string) => {
  return val.replace(/\-(\w)/g, (all, letter) => {
    return letter.toUpperCase();
  });
};

/**
 * 首字母转大写
 * @param str
 * @returns string
 */
export const firstToUpper = (str: string) => {
  return str.slice(0, 1).toLocaleUpperCase() + str.slice(1);
};
